package com.ts.main.admin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域
 * @author lk
 */
@Configuration
public class CorsConfig {
    /**
     * 解决跨域
     *
     * @return
     */
    @Bean
    public CorsFilter corsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 设置允许的方法
        config.addAllowedMethod("*");
        // 设置允许跨域请求的域名
//        config.addAllowedOrigin("*");
        config.addAllowedOriginPattern("*");
        //证书
        config.setAllowCredentials(true);
        // 设置允许请求的请求头
        config.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 设置允许跨域的路由
        source.registerCorsConfiguration("/**",config);
        return new CorsFilter(source);
    }
}
